#Clear R
rm(list=ls())

load("Study2_data.Rdta")

#function to install packages if they don't exist
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("ggplot2", "psy","ggplot2","psych","interplot","stargazer","dplyr","stringr","tidyr","lavaan","moments","lm.beta", "melt")
ipak(packages)

#start zero function - needed to run the models below
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#############################
#
#subset - exclude those who could not hear/see the treatment
#
#############################
nc <- subset(nc,dumHearSee==0)

########################################################
#
#Appendix B.2 Study2: Psychometric properties of the Need for Cognition
#
########################################################

########################################################
#Table B.12: Study 2: Descriptive statistics Need for Cognition
########################################################

#18-item battery
mean(nc$comb_NFC_full, na.rm=T)
SD(nc$comb_NFC_full, na.rm=T)
kurtosis(nc$comb_NFC_full, na.rm=T)
skewness(nc$comb_NFC_full, na.rm=T)
var(nc$comb_NFC_full,na.rm=T)

NFC18 <- (data.frame(nc$nfc1, nc$nfc2, nc$nfc3, nc$nfc4, nc$nfc5, nc$nfc6, nc$nfc7, nc$nfc8, nc$nfc9, nc$nfc10, nc$nfc11, nc$nfc12, nc$nfc13, nc$nfc14, nc$nfc15, nc$nfc16, nc$nfc17, nc$nfc18))
cronbach(NFC18)

#6-item Bullock
mean(nc$comb_NFC_Bullock, na.rm=T)
SD(nc$comb_NFC_Bullock, na.rm=T)
kurtosis(nc$comb_NFC_Bullock, na.rm=T)
skewness(nc$comb_NFC_Bullock, na.rm=T)
var(nc$comb_NFC_Bullock,na.rm=T)
NFC6 <- (data.frame(nc$nfc1, nc$nfc2, nc$nfc3, nc$nfc6, nc$nfc8, nc$nfc16))
cronbach(NFC6)

#2-item ANES
mean(nc$comb_NFC_ANES, na.rm=T)
SD(nc$comb_NFC_ANES, na.rm=T)
kurtosis(nc$comb_NFC_ANES, na.rm=T)
skewness(nc$comb_NFC_ANES, na.rm=T)
var(nc$comb_NFC_ANES,na.rm=T)
ANES2 <- (data.frame(nc$nfc1, nc$nfc2))
cronbach(ANES2)

########################################################
#Table B.13 Study 2: Convergent validity of the Need for Cognition
########################################################

cor(nc$comb_NFC_full,nc$comb_NFC_Bullock,use="pairwise.complete.obs")
cor(nc$comb_NFC_full,nc$comb_NFC_ANES,use="pairwise.complete.obs")
cor(nc$comb_NFC_Bullock,nc$comb_NFC_ANES,use="pairwise.complete.obs")

########################################################
#Figure B5. Study 2: Histograms of distributions of Need for Cognition measured with different batteries
########################################################

forggplot_NFC<- melt(nc,measure.vars = c("comb_NFC_ANES","comb_NFC_Bullock","comb_NFC_full")) 
ggplot(forggplot_NFC,aes(x=value,colour=variable, fill=variable)) +  scale_colour_manual(values=c("red","chartreuse2", "blue")) + labs(title="Need for Cognition", x="Need for Cognition", y="Frequency") +geom_histogram(, binwidth=.02, alpha=1, position=position_dodge()) + scale_fill_manual(values=c("red","chartreuse2", "blue"), labels=c("ANES 2-item", "Bullock 6-item","Cacioppo et al. 18-item"), guide=guide_legend(title = "Battery"))  + guides(colour=FALSE) + theme_bw()

########################################################
#Table B14: Study 2: Kolmogorov-Smirnov test comparing distributions of full and truncated measures
########################################################

ks.test(nc$comb_NFC_ANES, nc$comb_NFC_full)
ks.test(nc$comb_NFC_Bullock, nc$comb_NFC_full)

########################################################
#Table B15: Study 2: Confirmatory Factor Analyses of the 18 item battery Cacioppo
########################################################

#fit 
CF18<-'NFC_full18 = ~ NA*nfc1+ nfc2+nfc3+nfc4+nfc5+nfc6+nfc7+nfc8+nfc9+nfc10+nfc11+nfc12+nfc13+nfc14+nfc15+nfc16+nfc17+nfc18
# fix variance of speed factor
NFC_full18 ~~ 1*NFC_full18'

#assess fit
fit<-cfa(CF18,ordered=c("nfc1", "nfc2", "nfc3", "nfc4", "nfc5", "nfc6","nfc7", "nfc8", "nfc9", "nfc10", "nfc11", "nfc12", "nfc13", "nfc14", "nfc15", "nfc16", "nfc17", "nfc18"),data=nc)
summary(fit, fit.measures=TRUE, standardized=T)
inspect(fit,what="std")

########################################################
#
#Appendix B.3 Study 2: Randomization checks
#
########################################################

########################################################
#Table B16: Study 2: Check if exclusion of participants is randomly distributed across conditions
########################################################

# See first lines of "Study2_results.R" script for the output of this appendix. There we subset the data to those who could hear and see the film.

########################################################
#Table B17: Study 2: Randomization check
########################################################

random18<-lm(comb_NFC_full~t_message+t_partycue+t_gender+t_aggressive,subset(nc))
random6<-lm(comb_NFC_Bullock~t_message+t_partycue+t_gender+t_aggressive,subset(nc))
random2<-lm(comb_NFC_ANES~t_message+t_partycue+t_gender+t_aggressive,subset(nc))

stargazer(random2, random6, random18, title="test", align=TRUE, 
          dep.var.labels=c("ANES 2-item", "Bullock 6-item", "Cacioppo et al. 18-item"), 
          omit.stat=c("LL","ser","f", "adj.rsq"), 
          covariate.labels = c("Policy Information", "Ideological Cue", "Gender",
                               "Aggressive Speech", "Constant"),
          star.cutoffs=c(0.05), 
          notes = "Unstandardized coefficients from OLS Regression models; *p<0.05", 
          notes.append = FALSE, no.space=TRUE)


########################################################
#
#Appendix B.4 Study 2: Results from Figure 3 from main text
#
########################################################

########################################################
#Table B18: Study 2: OLS regression results Figure 3 main text
########################################################

#run models
p1 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_ANES*t_message+t_partycue*comb_NFC_ANES+t_gender*comb_NFC_ANES+t_aggressive*comb_NFC_ANES,subset(nc))
p2 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_Bullock*t_message+t_partycue*comb_NFC_Bullock+t_gender*comb_NFC_Bullock+t_aggressive*comb_NFC_Bullock,subset(nc))
p3 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_full*t_message++t_partycue*comb_NFC_full+t_gender*comb_NFC_full+t_aggressive*comb_NFC_full,subset(nc))

########################################################
# Table B19: Study 2: Standardized regression results Figure 3 main text
########################################################

#run models
p1 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_ANES*t_message+t_partycue*comb_NFC_ANES+t_gender*comb_NFC_ANES+t_aggressive*comb_NFC_ANES,subset(nc))
p2 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_Bullock*t_message+t_partycue*comb_NFC_Bullock+t_gender*comb_NFC_Bullock+t_aggressive*comb_NFC_Bullock,subset(nc))
p3 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_full*t_message++t_partycue*comb_NFC_full+t_gender*comb_NFC_full+t_aggressive*comb_NFC_full,subset(nc))

Anes.beta<-lm.beta(p1)
Bullock.beta<-lm.beta(p2)
Full.beta<-lm.beta(p3)

########################################################
#
#B.5 Study 2: Accounting for measurement error
#
########################################################

#select variables for analsyses
subset_nc <- nc[, c(81:98, 100:104)]
#exclude missing values
finalData <- na.omit(subset_nc) 

########################################################
#Table B20: Study 2: NfC and Policy Cues accounting for measurement error
########################################################

############################
#18-item
############################
#fit 
CF18<-'NFC_full18 = ~ NA*nfc1+ nfc2+nfc3+nfc3+nfc4+nfc5+nfc6+nfc7+nfc8+nfc8+nfc9+nfc10+nfc11+nfc12+nfc13+nfc14+nfc15+nfc16+nfc17+nfc18
# fix variance of speed factor
NFC_full18 ~~ 1*NFC_full18'

#assess fit
fit<-cfa(CF18,ordered=c("nfc1", "nfc2", "nfc3", "nfc4", "nfc5", "nfc6","nfc7", "nfc8", "nfc9", "nfc10", "nfc11", "nfc12", "nfc13", "nfc14", "nfc15", "nfc16", "nfc17", "nfc18"),data=finalData)

#create 18-item NFC battery based upon estimates from CFA
finalData$LAV_NFC18<-(lavPredict(fit, type = "lv", method="EBM", se.fit = FALSE, label = TRUE, optim.method = "nlminb"))

#fit model
LAV_Full18 <- lm(zero1(dv_agree_partyposition)~LAV_NFC18*t_message+t_partycue*LAV_NFC18+t_gender*LAV_NFC18+t_aggressive*LAV_NFC18,data=finalData)

#Run model
interplot(m = LAV_Full18, var1 = "t_message", var2 = "LAV_NFC18", hist=TRUE) + theme_bw() + geom_hline(yintercept = 0, linetype = "solid") + xlab("18-item Need for Cognition") + ylab("Agreement with policy")


#6-item
#fit 
CF6<-'NFC_full6 = ~ NA*nfc1+ nfc2+nfc3+nfc6+nfc8+nfc16
# fix variance of speed factor
NFC_full6 ~~ 1*NFC_full6'

#assess fit
fit<-cfa(CF6,ordered=c("nfc1", "nfc2", "nfc3", "nfc6", "nfc8", "nfc16"),data=finalData)

#create 6-item NFC battery based upon estimates from CFA
finalData$LAV_NFC6<-(lavPredict(fit, type = "lv", method="EBM", se.fit = FALSE, label = TRUE, optim.method = "nlminb"))

#fit model
LAV_Full6 <- lm(zero1(dv_agree_partyposition)~LAV_NFC6*t_message+t_partycue*LAV_NFC6+t_gender*LAV_NFC6+t_aggressive*LAV_NFC6,data=finalData)
summary(LAV_Full6)
#Run model
interplot(m = LAV_Full6, var1 = "t_message", var2 = "LAV_NFC6", hist=TRUE) + theme_bw() + geom_hline(yintercept = 0, linetype = "solid") + xlab("6-item Need for Cognition") + ylab("Agreement with policy")

########################################################
#
#Appendix B.6 Study 2: Results conditional upon political ideology
#
########################################################

########################################################
#Figure B6. Need for Cognition and Policy Cues among Conservatives
########################################################

#create left-right variable
nc$lr_ideology <- nc$cv12e101

#create left-dummy
nc$left[nc$lr_ideology>=0 & nc$lr_ideology<=4]=1
nc$left[nc$lr_ideology>=5 & nc$lr_ideology<=10]=0

#create right-dummy
nc$right[nc$lr_ideology>=6 & nc$lr_ideology<=10]=1
nc$right[nc$lr_ideology<6]=0

########################################################
#Treatment effect among left and right
########################################################
treatment_left <-       lm(zero1(dv_agree_partyposition)~comb_NFC_full + t_message+t_partycue +t_gender+t_aggressive,data=nc, left==1)
treatment_right <-       lm(zero1(dv_agree_partyposition)~comb_NFC_full + t_message+t_partycue +t_gender+t_aggressive,data=nc, right==1)

#main effect of policy information for conservatives and liberals
cons <-       lm(zero1(dv_agree_partyposition)~t_message+t_partycue+t_gender+t_aggressive,data=nc, right==1)
summary(cons)
libs <-       lm(zero1(dv_agree_partyposition)~t_message+t_partycue+t_gender+t_aggressive,data=nc, left==1)
summary(libs)


########################################################
#Figure B6. Need for Cognition and Policy Cues among Conservatives
########################################################

#run models on subset of respondents who are right-leaning
p1 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_ANES*t_message+t_partycue*comb_NFC_ANES+t_gender*comb_NFC_ANES+t_aggressive*comb_NFC_ANES,data=nc, right==1)
summary(p1)
p2 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_Bullock*t_message+t_partycue*comb_NFC_Bullock+t_gender*comb_NFC_Bullock+t_aggressive*comb_NFC_Bullock,data=nc, right==1)
summary(p2)
p3 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_full*t_message++t_partycue*comb_NFC_full+t_gender*comb_NFC_full+t_aggressive*comb_NFC_full,data=nc, right==1)
summary(p3)
#creat plot
p1inter <- interplot(m = p1, var1 = "t_message", var2 = "comb_NFC_ANES")$data
p2inter <- interplot(m = p2, var1 = "t_message", var2 = "comb_NFC_Bullock")$data
p3inter <- interplot(m = p3, var1 = "t_message", var2 = "comb_NFC_full")$data
p1inter$index <- "ANES 2-item"
p2inter$index <- "Bullock 6-item"
p3inter$index <- "Cacioppo et al. 18-item"

forplot <- data.frame(rbind(p1inter,p2inter,p3inter))
forplot$index <- factor(forplot$index,levels=c("ANES 2-item","Bullock 6-item","Cacioppo et al. 18-item"))
ggplot(forplot, aes(x=fake,y=coef1))+geom_line()+geom_ribbon(aes(ymax=ub,ymin=lb),alpha=.4)+facet_wrap(~index)+theme_bw()+ylab("Marginal Effect of Policy Information")+xlab("Need for Cognition")

########################################################
#Figure B7. Need for Cognition and Policy Cues among liberals
########################################################

#run models on subset of respondents who are left-leaning
p1 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_ANES*t_message+t_partycue*comb_NFC_ANES+t_gender*comb_NFC_ANES+t_aggressive*comb_NFC_ANES,data=nc, left==1)
summary(p1)
p2 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_Bullock*t_message+t_partycue*comb_NFC_Bullock+t_gender*comb_NFC_Bullock+t_aggressive*comb_NFC_Bullock,data=nc, left==1)
p3 <-       lm(zero1(dv_agree_partyposition)~comb_NFC_full*t_message++t_partycue*comb_NFC_full+t_gender*comb_NFC_full+t_aggressive*comb_NFC_full,data=nc, left==1)
summary(p3)
#create plot
p1inter <- interplot(m = p1, var1 = "t_message", var2 = "comb_NFC_ANES")$data
p2inter <- interplot(m = p2, var1 = "t_message", var2 = "comb_NFC_Bullock")$data
p3inter <- interplot(m = p3, var1 = "t_message", var2 = "comb_NFC_full")$data
p1inter$index <- "ANES 2-item"
p2inter$index <- "Bullock 6-item"
p3inter$index <- "Cacioppo et al. 18-item"

forplot <- data.frame(rbind(p1inter,p2inter,p3inter))
forplot$index <- factor(forplot$index,levels=c("ANES 2-item","Bullock 6-item","Cacioppo et al. 18-item"))
ggplot(forplot, aes(x=fake,y=coef1))+geom_line()+geom_ribbon(aes(ymax=ub,ymin=lb),alpha=.4)+facet_wrap(~index)+theme_bw()+ylab("Marginal Effect of Policy Information")+xlab("Need for Cognition")







